<script setup>
import { ref } from 'vue'
import {useRoute} from "vue-router";
import axios from "@/utils/axios.js"

const route = useRoute()
// 5个密保问题选项
const questionOptions = [
  { value: '您最喜欢的书籍是什么？', label: '您最喜欢的书籍是什么？' },
  { value: '您的第一个宠物的名字是什么？', label: '您的第一个宠物的名字是什么？' },
  { value: '您的小学校名是什么？', label: '您的小学校名是什么？' },
  { value: '您的出生城市是哪里？', label: '您的出生城市是哪里？' },
  { value: '您母亲的娘家姓是什么？', label: '您母亲的娘家姓是什么？' }
]
const us = JSON.parse(sessionStorage.getItem("user"));
// 三个密保问题的数据
const securityQuestions = ref([
  { userId: us.userId, question: '', answer: '' },
  { userId: us.userId, question: '', answer: '' },
  { userId: us.userId, question: '', answer: '' }
])

// 保存设置
const saveSettings = () => {
  for (let i = 0; i < securityQuestions.value.length; i++) {
    const item = securityQuestions.value[i]
    // 判断用户的输入问题
    if (!item.question) {
      ElMessage.error(`请选择第${i + 1}个密保问题`)
      return
    }

    if (!item.answer) {
      ElMessage.error(`请输入第${i + 1}个问题的答案`)
      return
    }
  }
  axios.post(`api/securityQuestions/setQuestions`,securityQuestions.value)
      .then((res) => {
        if(res.code === 200) {
          ElMessage({
            message:res.msg,
            duration:1500,
            type: 'success'
          })
        }else{
          ElMessage({
            message:res.msg,
            duration:1500,
            type: 'error'
          })
        }
      })
}
</script>

<template>
  <van-nav-bar
      title="设置密保问题"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
      fixed
      safe-area-inset-top
  />
  <div class="security-questions-page">
    <div class="page-header">
      <button class="back-button" @click="$router.back()">
        <i class="el-icon-arrow-left"></i>
      </button>
    </div>

    <div class="tip-bar">
      <i class="el-icon-warning-outline tip-icon"></i>
      <span>请选择您容易记住但他人难以猜测的问题和答案</span>
    </div>

    <div class="questions-container">
      <div
          v-for="(item, index) in securityQuestions"
          :key="item.id"
          class="question-item"
      >
        <div class="question-number">{{ item.id }}</div>
        <div class="question-content">
          <h3>选择问题</h3>

          <div class="form-item">
            <el-select
                v-model="item.question"
                placeholder="请选择一个密保问题"
                clearable
            >
              <el-option
                  v-for="option in questionOptions"
                  :key="option.value"
                  :label="option.label"
                  :value="option.value"
              />
            </el-select>
          </div>

          <div class="form-item">
            <el-input
                v-model="item.answer"
                placeholder="请输入答案"
                clearable
            >
              <template #prepend>您的答案</template>
            </el-input>
          </div>

        </div>
      </div>
    </div>

    <div class="save-btn-container">
      <el-button
          type="primary"
          style="width: 100%; background: linear-gradient(to right, #ff6034, #ff976a); border: none;"
          @click="saveSettings"
      >
        保存设置
      </el-button>
    </div>
  </div>
</template>

<style scoped>
.security-questions-page {
  min-height: 100vh;
  padding: 16px;
  max-width: 600px;
  margin: 0 auto;
  background-color: #f7f8fa;
}

.page-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding: 10px 0;
}

.back-button {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  margin-right: 10px;
  color: #606266;
}

.page-title {
  font-size: 18px;
  font-weight: 600;
}

.tip-bar {
  display: flex;
  align-items: center;
  background-color: #fff8e7;
  color: #e6a23c;
  padding: 12px 16px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 14px;
}

.tip-icon {
  margin-right: 8px;
}

.questions-container {
  margin-bottom: 24px;
}

.question-item {
  display: flex;
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.question-number {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 24px;
  height: 24px;
  background-color: #ff976a;
  color: white;
  border-radius: 50%;
  margin-right: 12px;
  font-weight: bold;
  flex-shrink: 0;
}

.question-content {
  flex: 1;
}

.question-content h3 {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.form-item {
  margin-bottom: 18px;
}

.answer-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 6px;
  margin-left: 80px;
}

.save-btn-container {
  margin-top: 24px;
}
</style>